package com.slicktechnologies.server.addhocprinting;

import java.text.SimpleDateFormat;
import java.util.List;
import java.net.URL;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Image;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPRow;
import com.itextpdf.text.pdf.PdfPTable;
import com.slicktechnologies.server.GenricServiceImpl;
import com.slicktechnologies.shared.Contract;
import com.slicktechnologies.shared.Quotation;
import com.slicktechnologies.shared.Sales;
import com.slicktechnologies.shared.SalesLineItem;
import com.slicktechnologies.shared.common.businessprocesslayer.Customer;
import com.slicktechnologies.shared.common.businessunitlayer.Company;
import com.slicktechnologies.shared.common.helperlayer.DocumentUpload;
import static com.googlecode.objectify.ObjectifyService.ofy;


public class QuotationPdf {
	Sales qp;
	
	List<SalesLineItem> products;
	Customer cust;
	Company comp;
	public  Document document;
	public GenricServiceImpl impl;
	public int serviceno=1;
	private  Font font16boldul,font12bold,font8bold,font8,font12boldul,font12;
	private SimpleDateFormat fmt= new SimpleDateFormat("dd-MMM-yyyy");
	
	public 	QuotationPdf()
	{
		font16boldul = new Font(Font.FontFamily.HELVETICA  , 16, Font.BOLD| Font.UNDERLINE);
		new Font(Font.FontFamily.HELVETICA,12,Font.BOLD|Font.UNDERLINE);
		font12bold= new Font(Font.FontFamily.HELVETICA,12,Font.BOLD);
		font8bold = new Font(Font.FontFamily.HELVETICA,8,Font.BOLD);
		font8= new Font(Font.FontFamily.HELVETICA,8);
		font12boldul= new Font(Font.FontFamily.HELVETICA,12,Font.BOLD|Font.UNDERLINE);
		font12=new Font(Font.FontFamily.HELVETICA,12);
		impl= new GenricServiceImpl();
	}
	//PLASE TO APPLY COMPANY SPECIFEC SETTINGS
	public void getContract(long count)
	{
		
		//Load Contract 
	   
		qp=ofy().load().type(Contract.class).id(count).now();
		
		int contractCount=qp.getCount();
		
		
	   //Load Customer
		if(qp.getCompanyId()!=null)
	      cust=ofy().load().type(Customer.class).filter("count",qp.getCustomerId()).
	        filter("companyId",qp.getCompanyId()).first().now();
		if(qp.getCompanyId()==null)
		      cust=ofy().load().type(Customer.class).filter("count",qp.getCustomerId()).first().now();
		
		if(qp.getCompanyId()!=null)
			 comp=ofy().load().type(Company.class).filter("companyId",qp.getCompanyId()).first().now();
		else
			comp=ofy().load().type(Company.class).first().now();
	   
		products=qp.getItems();
		
		System.out.println("Comp val "+comp);
	   
		
		
		
		
	}
	
	public void getQuotation(long count)
	{
		//Load Contract 
		
			qp=ofy().load().type(Quotation.class).id(count).now();
			int contractCount=qp.getCount();
			
			
		   //Load Customer
			if(qp.getCompanyId()!=null)
		      cust=ofy().load().type(Customer.class).filter("count",qp.getCustomerId()).
		        filter("companyId",qp.getCompanyId()).first().now();
			if(qp.getCompanyId()==null)
			      cust=ofy().load().type(Customer.class).filter("count",qp.getCustomerId()).first().now();
			
			if(qp.getCompanyId()!=null)
				 comp=ofy().load().type(Company.class).filter("companyId",qp.getCompanyId()).first().now();
			else
				comp=ofy().load().type(Company.class).first().now();
		   
			products=qp.getItems();
	}
	
	
	
	
	
	
	public  void createPdf() {
		 
		 showLogo(document, comp);    
		 createCompanyHedding();
	     createCompanyAdress();
	   
	      
	      try {
			createHeaderTable();
		} catch (DocumentException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	      ceateComments();
	      createProductDetail();
	      createPaymentFooter();
	      addFooter();	  
	     
	    }
	
	
	public  void createCompanyHedding()
	{
		
		
		
		Phrase companyName= new Phrase(comp.getBusinessUnitName(),font16boldul);
	     Paragraph p =new Paragraph();
	    
	     p.add(Chunk.NEWLINE);
	     p.add(companyName);
	     p.setAlignment(Element.ALIGN_CENTER);
	     try {
			document.add(p);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public  void createCompanyAdress()
	{
		
		Phrase adressline1= new Phrase(comp.getAddress().getAddrLine1(),font12);
		Phrase adressline2=null;
		if(comp.getAddress().getAddrLine2()!=null)
		{
			adressline2= new Phrase(comp.getAddress().getAddrLine2(),font12);
		}
		
		
		Phrase landmark=null;
		Phrase locality=null;
		if(comp.getAddress().getLandmark()!=null&&comp.getAddress().getLocality().equals("")==false)
		{
			
			String landmarks=comp.getAddress().getLandmark()+" , ";
			locality= new Phrase(landmarks+comp.getAddress().getLocality()+" , "+comp.getAddress().getCity()+" , "
				      +comp.getAddress().getPin(),font12);
		}
		else
		{
			locality= new Phrase(comp.getAddress().getLocality()+" , "+comp.getAddress().getCity()+" , "
				      +comp.getAddress().getPin(),font12);
		}
		Paragraph adressPragraph=new Paragraph();
		adressPragraph.add(adressline1);
		adressPragraph.add(Chunk.NEWLINE);
		if(adressline2!=null)
		{
				adressPragraph.add(adressline2);
				adressPragraph.add(Chunk.NEWLINE);
		}
		
		
		
		adressPragraph.add(locality);
		
		adressPragraph.setAlignment(Element.ALIGN_CENTER);
		adressPragraph.setSpacingBefore(10);
		adressPragraph.setSpacingAfter(10);
		
		
		// Phrase for phone,landline ,fax and email
		
		Phrase titlecell=new Phrase("Mob :",font8bold);
		Phrase titleTele = new Phrase("Tele :",font8bold);
		Phrase titleemail= new Phrase("Email :",font8bold);
		Phrase titlefax= new Phrase("Fax :",font8bold);
		
		Phrase titleservicetax=new Phrase("Service Tax No     :",font8bold);
		Phrase titlevatatx=new Phrase("Vat Tax No :    ",font8bold);
		String servicetax=comp.getServiceTaxNo();
		String vatttax=comp.getVatTaxNo();
		Phrase servicetaxphrase=null;
		Phrase vattaxphrase=null;
		
		if(servicetax!=null&&servicetax.trim().equals("")==false)
		{
			servicetaxphrase=new Phrase(servicetax,font8);
		}
			

		if(vatttax!=null&&vatttax.trim().equals("")==false)
		{
			vattaxphrase=new Phrase(vatttax,font8);
		}
		
	
		
		
		// cell number logic
		String stringcell1=comp.getContact().get(0).getCellNo1()+"";
		String stringcell2=null;
		Phrase mob=null;
		if(comp.getContact().get(0).getCellNo2()!=-1)
			stringcell2=comp.getContact().get(0).getCellNo2()+"";
		if(stringcell2!=null)
		    mob=new Phrase(stringcell1+" / "+stringcell2,font8);
		else
			 mob=new Phrase(stringcell1,font8);
		
		
		//LANDLINE LOGIC
		Phrase landline=null;
		if(comp.getContact().get(0).getLandline()!=-1)
			landline=new Phrase(comp.getContact().get(0).getLandline()+"",font8);
		
		
		
		
		// fax logic
		Phrase fax=null;
		if(comp.getContact().get(0).getFaxNo()!=null)
			fax=new Phrase(comp.getContact().get(0).getFaxNo()+"",font8);
		// email logic
		Phrase email= new Phrase(comp.getContact().get(0).getEmail(),font8);
		
		adressPragraph.add(Chunk.NEWLINE);
		adressPragraph.add(titlecell);
		adressPragraph.add(mob);
		adressPragraph.add(new Chunk("            "));
		
		
		if(landline!=null)
		{
			
			adressPragraph.add(titleTele);
			adressPragraph.add(landline);
			adressPragraph.add(new Chunk("            "));
		}
		
		
		
		if(fax!=null)
		{
			
			adressPragraph.add(titlefax);
			adressPragraph.add(fax);
			adressPragraph.add(Chunk.NEWLINE);
		}
		
		
		adressPragraph.add(titleemail);
		adressPragraph.add(new Chunk("            "));
		adressPragraph.add(email);
		adressPragraph.add(Chunk.NEWLINE);
		
		if(servicetaxphrase!=null)
		{
			adressPragraph.add(titleservicetax);
			adressPragraph.add(servicetaxphrase);
			
		}
		
		if(vattaxphrase!=null)
		{
			adressPragraph.add(titlevatatx);
			adressPragraph.add(vattaxphrase);
			adressPragraph.add(new Chunk("            "));
			
		}
		
		
		
		
		adressPragraph.add(Chunk.NEWLINE);
		String title="";
		if(qp instanceof Contract)
			title="Tax Invoice";
			
		else
			title="Quotation";
		
		Phrase Ptitle= new Phrase(title,font12bold);
		adressPragraph.add(Chunk.NEWLINE);
		adressPragraph.add(Chunk.NEWLINE);
		adressPragraph.add(Ptitle);
		adressPragraph.add(Chunk.NEWLINE);
		adressPragraph.add(Chunk.NEWLINE);
		
		try {
			document.add(adressPragraph);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public  void createHeaderTable() throws DocumentException
	{
		 Phrase nameTitle = new Phrase("Name :",font8bold);
		 Phrase phoneTitle= new Phrase("Phone :",font8bold);
		 Phrase adressTitle= new Phrase("Adress :",font8bold);
		 Phrase emailTitle= new Phrase("Email :",font8bold);
		 
		 PdfPCell titlecustnameCell=new PdfPCell();
		 titlecustnameCell.addElement(nameTitle);
		 PdfPCell titlecustPhonecell = new PdfPCell();
		 titlecustPhonecell.addElement(phoneTitle);
		 PdfPCell titleadressCell = new PdfPCell();
		 titleadressCell.addElement(adressTitle);
		 PdfPCell titleemailCell = new PdfPCell(emailTitle);
		 titleemailCell.addElement(emailTitle);
		
		 
		
		Phrase customername = new Phrase(cust.getFullname(),font8);
		Phrase phonechunk = new Phrase(cust.getCellNumber1()+"",font8);
		Phrase emailchunk = new Phrase(cust.getEmail()+"",font8);
		
		Phrase blankphrase = new Phrase("  ");
		PdfPCell blankcell = new PdfPCell(blankphrase);
		
		Phrase customeradress= new Phrase(cust.getAdress().getAddrLine1(),font8);
		Phrase customeradress2=null;
		
		//Patch
		if(cust.getAdress().getAddrLine2()!=null)
		   customeradress2= new Phrase(cust.getAdress().getAddrLine2(),font8);
		Phrase landmark=null;
		Phrase locality= null;
		
		
		if(cust.getAdress().getLandmark()!=null)
		{
		    landmark = new Phrase(cust.getAdress().getLandmark());
		    locality=new Phrase(landmark+" "+cust.getAdress().getLocality(),font8);
		}
		else
			locality=new Phrase(cust.getAdress().getLocality(),font8);
		
		Phrase cityState=new Phrase(cust.getAdress().getCity()
				+" "+cust.getAdress().getState(),font8);
		Phrase country=new Phrase("  "+cust.getAdress().getCountry(),font8);
		Phrase pin=new Phrase("  "+cust.getAdress().getPin()+"",font8);
		 
		
		Paragraph adresspara = new Paragraph();
		 adresspara.add(customeradress);
		 adresspara.add(Chunk.NEWLINE);
		 if(customeradress2!=null)
			 adresspara.add(customeradress2);
		 adresspara.add(locality);
		 adresspara.add("  ");
		 adresspara.add(cityState);
		 adresspara.add("   ");
		 adresspara.add(pin);
		 adresspara.add(Chunk.NEWLINE);
		 adresspara.add(country);
		
		 
		 PdfPCell custnameCell=new PdfPCell();
		 custnameCell.addElement(customername);
		 PdfPCell custPhonecell = new PdfPCell();
		 custPhonecell.addElement(phonechunk);
		 PdfPCell adressCell = new PdfPCell();
		 adressCell.addElement(adresspara);
		 PdfPCell emailCell = new PdfPCell(emailTitle);
		 emailCell.addElement(emailchunk);
		
		String header;
		header = "ID :";
		
		Phrase billno = new Phrase(header,font8bold);
		Phrase orederdate= new Phrase("Date :",font8bold);
		
		 PdfPCell titlebillCell=new PdfPCell();
		 titlebillCell.addElement(billno);
		 PdfPCell titledatecell = new PdfPCell();
		 titledatecell.addElement(orederdate);
		 
		 
		 
		Phrase realqno=null;
	    realqno = new Phrase(qp.getCount()+"",font8);
		
		
		String date=fmt.format(qp.getCreationDate());
		Phrase realorederdate= new Phrase(date+"",font8);
		realorederdate.trimToSize();
		
	
		
		
		
		
		
		PdfPCell billCell=new PdfPCell();
		billCell.addElement(realqno);
		
		PdfPCell datecell = new PdfPCell();
		datecell.addElement(realorederdate);
		
		PdfPCell durationCell = new PdfPCell();
		PdfPCell noServicesCell = new PdfPCell();
		
		
		
		
	     
		
		 PdfPTable table = new PdfPTable(4);
		 
		 // 1 st row name phone
		 table.addCell(titlecustnameCell);
		 table.addCell(custnameCell);
		 
		 table.addCell(titlecustPhonecell);
		 table.addCell(custPhonecell);
		 
		 
		 
		 // 2nd row adress and email
		 table.addCell(titleadressCell);
		 table.addCell(adressCell);
		 
		 table.addCell(titleemailCell);
		 table.addCell(emailCell);
		 
		table.setWidths(new float[]{18,32,18,32});
		table.setHorizontalAlignment(Element.ALIGN_LEFT);
		table.setWidthPercentage(100);
		 
		PdfPTable billtable = new PdfPTable(4);
		billtable.setWidths(new float[]{20,30,20,30});
		billtable.setWidthPercentage(100);
		billtable.setHorizontalAlignment(Element.ALIGN_LEFT);

		
		
		//1  row bill number and date
		 billtable.addCell(titlebillCell);
		 billtable.addCell(billCell);
		 billtable.addCell(titledatecell);
		 billtable.addCell(realorederdate);
		 
		
		
		 int row =3;
		
		 
		 int noCol=table.getNumberOfColumns();
		 for(int i=0;i<2;i++)
		 {
			 PdfPRow temp=table.getRow(i);
			PdfPCell[] cells= temp.getCells();
			for(PdfPCell cell:cells)
				cell.setBorder(Rectangle.NO_BORDER);
		}
		 
		 for(int i=0;i<billtable.getRows().size();i++)
		 {
			 PdfPRow temp=billtable.getRow(i);
			PdfPCell[] cells= temp.getCells();
			for(PdfPCell cell:cells)
				cell.setBorder(Rectangle.NO_BORDER);
		}
		 
		PdfPTable parenttable= new PdfPTable(2);
		parenttable.setWidthPercentage(100);
		parenttable.setWidths(new float[]{50,50});
		
		PdfPCell custinfocell = new PdfPCell();
		PdfPCell  billinfocell = new PdfPCell();
		
		custinfocell.addElement(table);
		billinfocell.addElement(billtable);
		parenttable.addCell(custinfocell);
		parenttable.addCell(billinfocell);
	
		document.add(parenttable);
         
         
	}
	
	
	public  void ceateComments()
	{
	
		Font font1 = new Font(Font.FontFamily.HELVETICA  , 8,Font.BOLD);
		Chunk commentChunk= new Chunk("Description :",font1);
		
		Chunk deschunk = null;
		if(qp.getDescription()!=null)
			deschunk= new Chunk(qp.getDescription(),font8);
	
		Paragraph para = new Paragraph();
		para.add(Chunk.NEWLINE);
		para.add(commentChunk);
		para.add(Chunk.NEWLINE);
		if(deschunk!=null)
				para.add(deschunk);
		para.add(Chunk.NEWLINE);
		para.add(Chunk.NEWLINE);
		try {
			document.add(para);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	
	public  void createProductDetail()
	{
		Font font1 = new Font(Font.FontFamily.HELVETICA  , 8|Font.BOLD);
		Phrase productdetails= new Phrase("Product Details",font1);
		Paragraph para = new Paragraph();
		para.add(productdetails);
		para.setAlignment(Element.ALIGN_CENTER);
			
		PdfPTable table = new PdfPTable(9);
		table.setWidthPercentage(100);
		try {
			table.setWidths(new int[]{100,100,150,170,170,200,100,100});
		} catch (DocumentException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		Phrase category = new Phrase("Category ",font1);
        Phrase brand = new Phrase("Brand ",font1);
        Phrase srNo = new Phrase("Serial Number",font1);
        Phrase prodName = new Phrase("Product Name ",font1);
       
        Phrase modelNo = new Phrase("Model Number",font1);
        Phrase deliveryDate = new Phrase("Expected Delievery Date",font1);
       
        Phrase rate= new Phrase ("Price",font1);
        Phrase servicetax = new Phrase("Service Tax",font1);
       
        Phrase total = new Phrase("Total",font1);
		
		 PdfPCell cellcategory = new PdfPCell(category);
         PdfPCell cellproduct = new PdfPCell(brand);
         PdfPCell cellsrNo = new PdfPCell(srNo);
         PdfPCell  cellProduct = new PdfPCell(prodName);
         PdfPCell modelno = new PdfPCell(modelNo);
         PdfPCell celldelieveryDate = new PdfPCell(deliveryDate);
         PdfPCell cellrate = new PdfPCell(rate);
         PdfPCell cellservicetax = new PdfPCell(servicetax);
         PdfPCell celltotal= new PdfPCell(total);
         
         
         
         table.addCell(cellcategory);
         table.addCell(cellproduct);
         table.addCell(cellsrNo);
         table.addCell(cellProduct);
         table.addCell(modelno);
         table.addCell(celldelieveryDate);
         table.addCell(cellrate);
         table.addCell(cellservicetax);
         table.addCell(celltotal);
         
         System.out.println("Size of Products "+products.size());
         for(int i=0;i<this.products.size();i++)
         {
        	 Phrase chunk=new Phrase(products.get(i).getCat(),font8);
        	 PdfPCell pdfcategcell = new PdfPCell(chunk);
        	 
        	 chunk = new Phrase(products.get(i).getBrand(),font8);
        	 PdfPCell pdfpbrandcell = new PdfPCell(chunk);
        	 
        	 chunk = new Phrase(products.get(i).getSrNo(),font8);
        	 PdfPCell pdfsrnocell = new PdfPCell(chunk);
        	 
        	 chunk = new Phrase(products.get(i).getProductName(),font8);
        	 PdfPCell pdfprodname = new PdfPCell(chunk);
        	 
        	 chunk = new Phrase(products.get(i).getModelNo(),font8);
        	 PdfPCell pdfmodelno = new PdfPCell(chunk);
        	 
        	 if(products.get(i).getExpectedDelDate()!=null)
        	   chunk = new Phrase(fmt.format(products.get(i).getExpectedDelDate()),font8);
        	 else
        		 chunk= new Phrase(("N.A"),font8);
        	 PdfPCell pdfexpectedDelDate = new PdfPCell(chunk);
        	 
        	 chunk = new Phrase(products.get(i).getPrice()+"",font8);
        	 PdfPCell pdfprice = new PdfPCell(chunk);
        	 
        	 chunk = new Phrase(products.get(i).getServiceTax()+"",font8);
        	 PdfPCell pdfServiceTax = new PdfPCell(chunk);
        	 
        	 
        	 double toatal=((products.get(i).getServiceTax()/100)*(products.get(i).getPrice()))+products.get(i).getPrice();
        	 
        	 chunk = new Phrase(toatal+"",font8);
        	 PdfPCell pdftotal = new PdfPCell(chunk);
        	 
        	 
        	
        	
        	 
        	 table.addCell(pdfcategcell);
        	 table.addCell(pdfpbrandcell);
        	 table.addCell(pdfsrnocell);
        	 table.addCell(pdfprodname);
        	 table.addCell(pdfmodelno);
        	 table.addCell(pdfexpectedDelDate);
        	 table.addCell(pdfprice);
        	 table.addCell(pdfServiceTax);
        	 table.addCell(pdftotal);
        	 
        	 System.out.println("Added in Table ");
        	 
        	 
        	}
        
         
         try {
			document.add(table);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
         
         
      }

   
	public  void createPaymentFooter()
	{
	
		Quotation quot=null;
		Contract cont=null;
		if(qp instanceof Contract)
			cont=(Contract) qp;
		
		else
			quot=(Quotation) qp;
		
		 Quotation quotation;
		 if(quot!=null)
		     quotation=quot;
		 else
			 quotation=cont;
		 int row=4;
		 PdfPTable table = new PdfPTable(2);
		 table.setWidthPercentage(30);
		
		 
		 Phrase phrasetotal =        new Phrase("Total   :",font8bold);
		 Phrase phraseperdiscount =  new Phrase("% Discount    :",font8bold);
		 Phrase phraseflatdiscount = new Phrase("Flat Discount :",font8bold);
		 Phrase phrasefinaltotal =   new Phrase("Net Payable   :",font8bold);
		 Phrase paidamt =   new Phrase("Amount Paid   :",font8bold);
		 Phrase balance =   new Phrase("Balance   :",font8bold);
		 
		
		 Phrase realdiscount, realflatdiscount,finaltotal;
		 //Calculate Discount in AMT
		 
		 if(quot!=null)
		 {
		Double grandTotal= quot.getGrandTotal();
		double discountAmount=grandTotal*quot.getPerDiscount()/100;
		int disc=(int) discountAmount;
		 realdiscount = new Phrase(disc +"( "+quotation.getPerDiscount()+" %)"+"",font8);
		 realflatdiscount = new Phrase(quotation.getFlatDiscount()+"",font8);
		 finaltotal = new Phrase(quotation.getNetpayable()+"",font8);
		 }
		 
		 else
		 {
		Double grandTotal= cont.getGrandTotal();
		double discountAmount=grandTotal*cont.getPerDiscount()/100;
		int disc=(int) discountAmount;
		 realdiscount = new Phrase(disc +"( "+cont.getPerDiscount()+" %)"+"",font8);
		 realflatdiscount = new Phrase(cont.getFlatDiscount()+"",font8);
		 finaltotal = new Phrase(cont.getNetpayable()+"",font8);
		 }
		 
		
		
		 Phrase amtpaid = null,amtbal = null;
		//Patch Patch
		
		if(cont!=null)
		
		{
			
			//amtpaid=new Phrase(cont.getTotalAmount()-cont.getbalance()+"",font8);
			//amtbal=new Phrase(cont.getbalance()+"",font8);
			
		}
		
		 Phrase realtotal = new Phrase(quotation.getTotalAmount()+"",font8);
		 table.addCell(phrasetotal);
		 table.addCell(realtotal);
		if(quotation.getPerDiscount()==0&&quotation.getFlatDiscount()==0)
			row=row-2;
		else if(quotation.getPerDiscount()!=0&&quotation.getFlatDiscount()==0)
		 {
			 
			 table.addCell(phraseperdiscount);
			 table.addCell(realdiscount);
			 table.addCell(phrasefinaltotal);
			 table.addCell(finaltotal);
			 row=row-1;
			 
		 }
		 
		 else if(quotation.getPerDiscount()==0&&quotation.getFlatDiscount()!=0)
		 {
			 
			 
			 table.addCell(phraseflatdiscount);
			 table.addCell(realflatdiscount);
			 table.addCell(phrasefinaltotal);
			 table.addCell(finaltotal);
			 row=row-1;
		 }
		 
		 else if(quotation.getFlatDiscount()!=0&&quotation.getPerDiscount()==0)
		 {
			 
			 table.addCell(phraseperdiscount);
			 table.addCell(realdiscount);
			 table.addCell(phraseflatdiscount);
			 table.addCell(realflatdiscount);
			 table.addCell(phrasefinaltotal);
			 table.addCell(finaltotal);
			 
		 }
		
		 else if(quotation.getFlatDiscount()!=0&& quotation.getPerDiscount()!=0)
		 {
			
			 table.addCell(phraseperdiscount);
			 table.addCell(realdiscount);
			 table.addCell(phraseflatdiscount);
			 table.addCell(realflatdiscount);
			 table.addCell(phrasefinaltotal);
			 table.addCell(finaltotal);
			 
		 }
		 
		/* if(qp instanceof Contract)
		 {
			 table.addCell(paidamt);
			 table.addCell(amtpaid);
			 table.addCell(balance);
			 table.addCell(amtbal);
			 row=row+2;
		 }*/
		 
		 Paragraph par = new Paragraph();
		 par.setSpacingBefore(40);
		 table.setHorizontalAlignment(Element.ALIGN_RIGHT);
		 
		 table.setSpacingBefore(20);
		 
		 System.out.println("Row value is "+row);
		 row=table.getRows().size();
		 for(int i=0;i<row;i++)
		 {
			 PdfPRow temp=table.getRow(i);
			PdfPCell[] cells= temp.getCells();
			for(PdfPCell cell:cells)
			{
				cell.setBorder(Rectangle.NO_BORDER);
			System.out.println("ajjj");
			}
		}
		 
		 try {
			document.add(table);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
		 
		
		 
	}
	
 
	
	
	
	
	public void addtaxparagraph()
	{
		Paragraph taxpara= new Paragraph();
		Phrase titleservicetax=new Phrase("Service Tax No     :",font8bold);
		Phrase titlevatatx=new Phrase("Vat Tax No :    ",font8bold);
		Phrase titlelbttax=new Phrase("LBT Tax No :    ",font8bold);
		Phrase space = new Phrase("									");
		String serv = null,lbt,vat = null;
		if(comp.getServiceTaxNo()==null)
			serv="";
		else
			serv=comp.getServiceTaxNo();
		if(comp.getVatTaxNo()==null)
			vat="";
		else
			vat=comp.getVatTaxNo();
		
		if(serv.equals("")==false)
		{
			taxpara.add(titleservicetax);
			Phrase servp= new Phrase(serv,font8);
			taxpara.add(servp);
		}
		taxpara.add(space);
		
		if(vat.equals("")==false)
		{
			taxpara.add("                ");
			taxpara.add(titlevatatx);
			Phrase vatpp= new Phrase(vat,font8);
			taxpara.add(vatpp);
		}  	 
		
		
		
		
		try {
			taxpara.setSpacingAfter(10);
			document.add(taxpara);
			
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public void addType()
	{
		Phrase header=null;
		if(qp instanceof Contract)
			header= new Phrase("Invoice",font12boldul);
			
		else
			header= new Phrase("Quotation",font12boldul);
		
		Paragraph para= new Paragraph(header);
		para.setAlignment(Element.ALIGN_CENTER);
		para.setSpacingBefore(10);
		para.setSpacingAfter(20);
		try {
			document.add(para);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	/*
	 *  This method extracts unique products in a hash set form services array list
	 *  we will create the returned list.
	 */
	
	
	
	/*
	 * This method takes a product name and returns a vector of services for that
	 * product name.
	 */
	
	
	
	public void addFooter()
	{
		 
		 Phrase recieverSinature = new Phrase("Reciever Signature",font12boldul);
		 Phrase blankphrase = new Phrase(" ");
		 Phrase blankphrase1 = new Phrase(" ");
		 Phrase blankphrase2 = new Phrase(" ");
		 Phrase companyname= new Phrase("For "+comp.getBusinessUnitName(),font12bold);
		 Phrase authorizedsignatory=null;
	     authorizedsignatory = new Phrase("AUTHORISED SIGNATORY"+"",font12boldul);
		 PdfPTable table = new PdfPTable(2);
		 try {
			table.setWidths(new float[]{50,50});
			table.setSpacingBefore(60);
		} catch (DocumentException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		 table.addCell(recieverSinature);
		 table.addCell(companyname);
		 table.addCell(blankphrase);
		 table.addCell(blankphrase1);
		 table.addCell(blankphrase2);
		 table.addCell(authorizedsignatory);
		 
		 table.setWidthPercentage(100);
		 
		 for(int i=0;i<3;i++)
		 {
			 PdfPRow temp=table.getRow(i);
			 
			PdfPCell[] cells= temp.getCells();
			for(PdfPCell cell:cells)
				cell.setBorder(Rectangle.NO_BORDER);
		}
		 
		
	
		try {
			document.add(table);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void addServiceDetail()
	{
		Phrase header=null;
		header= new Phrase("Service Detail",font12boldul);
		Paragraph para= new Paragraph(header);
		para.setAlignment(Element.ALIGN_LEFT);
		para.setSpacingBefore(20);
		para.setSpacingAfter(10);
		try {
			document.add(para);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	
	
	
	public static void showLogo(Document doc,Company comp)
	{
		DocumentUpload document =comp.getLogo();
		
		//patch
		String hostUrl; 
		String environment = System.getProperty("com.google.appengine.runtime.environment");
		if (environment.equals("Production")) {
		    String applicationId = System.getProperty("com.google.appengine.application.id");
		    String version = System.getProperty("com.google.appengine.application.version");
		    hostUrl = "http://"+version+"."+applicationId+".appspot.com/";
		} else {
		    hostUrl = "http://localhost:8888";
		}
		try {
			Image image2 = Image.getInstance(new URL(hostUrl+document.getUrl()));
			image2.scaleAbsolute(60f,60f);
			doc.add(image2);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		
		
				
	}
	
}
}

